package com.mingmen.canting.gongyingshang;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mingmen.canting.R;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.SharePreferenceUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;

@SuppressLint("ValidFragment")
public class SheDingFragment extends Fragment {
    @BindView(R.id.img_cooperation)
    ImageView imgCooperation;
    @BindView(R.id.img_heimigndan)
    ImageView imgHeimigndan;
    @BindView(R.id.lable_selectcanting)
    LinearLayout lableSelectcanting;
    Unbinder unbinder;
    private String mTitle;


    public static SheDingFragment getInstance(String title) {
        SheDingFragment sf = new SheDingFragment();
        sf.mTitle = title;
        return sf;
    }

    String cooperation;
    String blackList;
    String token;
    String supplierId;
String shippingFee;
@BindView(R.id.shippingFee)
TextView text_shippingFee;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fr_sheding, null);
        unbinder = ButterKnife.bind(this, v);
        EventBus.getDefault().register(this);
        cooperation = (String) SharePreferenceUtil.get(getActivity(), "cooperation", "");
        blackList = (String) SharePreferenceUtil.get(getActivity(), "blackList", "");
        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");
        supplierId = (String) SharePreferenceUtil.get(getActivity(), "supplierId", "");
        if (cooperation.equals("0")) {
            imgCooperation.setImageResource(R.mipmap.btn_close);
        } else {
            imgCooperation.setImageResource(R.mipmap.btn_open);
        }
        if (blackList.equals("0")) {
            imgHeimigndan.setImageResource(R.mipmap.btn_close);
        } else {
            imgHeimigndan.setImageResource(R.mipmap.btn_open);
        }
        shippingFee= (String) SharePreferenceUtil.get(getActivity(), "shippingFee", "");
        text_shippingFee.setText("￥"+shippingFee);
        return v;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.img_cooperation, R.id.img_heimigndan, R.id.lable_selectcanting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_cooperation:
                cooperation= (String) SharePreferenceUtil.get(getActivity(),"cooperation","");
                HttpResponse.setDefaultSupplier(token, supplierId, new StringCallback(getActivity()) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        BaseBean baseBean = new BaseBean();
                        baseBean = GsonUtil.GsonToBean(s, BaseBean.class);

                        if (baseBean.getCode() == 0) {
                            if (cooperation.equals("0")) {
                                imgCooperation.setImageResource(R.mipmap.btn_open);
//                                cooperation = (String) SharePreferenceUtil.get(getActivity(), "cooperation", "");
                                SharePreferenceUtil.put(getActivity(), "cooperation", "1");
                            } else {
                                imgCooperation.setImageResource(R.mipmap.btn_close);
                                SharePreferenceUtil.put(getActivity(), "cooperation", "0");
                            }
                            EventBus.getDefault().post(6);
                        }
                        Toast.makeText(getActivity(), baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                    }
                });

                break;
            case R.id.img_heimigndan:

                HttpResponse.SETBLACKLIST(token, supplierId, new StringCallback(getActivity()) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.d("SETBLACKLIST", s);
                        BaseBean baseBean = new BaseBean();
                        baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                        if (baseBean.getCode() == 0) {
                            if (blackList.equals("0")) {
                                imgHeimigndan.setImageResource(R.mipmap.btn_open);
                                blackList = "1";
                            } else {
                                imgHeimigndan.setImageResource(R.mipmap.btn_close);
                                blackList = "0";
                            }
                            EventBus.getDefault().post(6);
                        }
                    }
                });
                break;
            case R.id.lable_selectcanting:
                break;
        }
    }

    public void onEventMainThread(Integer integer) {


    }
}